package org.wingstudio.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.wingstudio.entity.File;
import org.wingstudio.entity.PageBean;
import org.wingstudio.entity.Source;
import org.wingstudio.service.SourceService;
import org.wingstudio.util.PageUtil;
import org.wingstudio.util.StringUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liao on 16-12-4.
 */

@Controller
@RequestMapping("/source")
public class SourceNormalController {

    @Resource
    private SourceService sourceService;

    @RequestMapping("/preview/{id}")
    public ModelAndView preview(@PathVariable("id")Integer id, HttpServletRequest request)
            throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        Source source=sourceService.findSourceById(id);
        modelAndView.addObject("source",source);
        modelAndView.setViewName("user/source/viewSource");
        return modelAndView;
    }
    @RequestMapping("/download/{id}")
    public ResponseEntity<byte[]> download(@PathVariable("id")Integer id) throws IOException {
        Source source=sourceService.findSourceById(id);
        source.setDownHit(source.getDownHit()+1);
        sourceService.update(source);
        String path="/opt/apache-tomcat-9.0.0.M10/webapps/Hgc/static/sourcePDF/"+source.getSourceName();
        java.io.File file=new java.io.File(path);
        HttpHeaders headers = new HttpHeaders();
        String fileName=new String(source.getSourceName().getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                headers, HttpStatus.CREATED);
    }

    @RequestMapping("/general")
    public ModelAndView general(@RequestParam(value="page",required=false)String page,
            HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("sourceTypeId",1);
        List<Source> sourceList=sourceService.listSource(map);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/source/general",
                sourceService.getTotal(map), Integer.parseInt(page), 10));
        modelAndView.addObject("sourceList",sourceList);
        modelAndView.setViewName("user/source/general");

        return modelAndView;
    }

    @RequestMapping("/learn")
    public ModelAndView learn(@RequestParam(value="page",required=false)String page,
                               HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("sourceTypeId",2);
        List<Source> sourceList=sourceService.listSource(map);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/source/learn",
                sourceService.getTotal(map), Integer.parseInt(page), 10));
        modelAndView.addObject("sourceList",sourceList);
        modelAndView.setViewName("user/source/learn");

        return modelAndView;
    }
}
